using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class MessageTypeManager
    {

        private MessageTypeManager() { }

        //public static void MessageTypeSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        //    {
        //        MessageTypesList messageTypes = new MessageTypesList();

        //        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
        //        nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

        //        XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
        //        HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);

        //        String description = xmlMessage.SelectSingleNode("//pnp:MessageTypeSearchRequest", nsmgr).InnerText;
        //        messageTypes = business.MessageTypeManager.MessageTypeSearch(messageTypeSearchRequest);  								
			
        //        resultMsgType = (int)MessageTypes.MessageTypeSearchResult;
        //        resultXmlMessage = new XmlDocument();
        //        resultXmlMessage.LoadXml(common.Utilities.SerializeObject(messageTypes, typeof(MessageTypesList)));
        //    }

        //MessageTypeSearchRequest 1043
        public static void MessageTypeSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            MessageTypesList typeList = new MessageTypesList();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            MessageTypeSearchRequest request = new MessageTypeSearchRequest();
            XmlNode searchNode = xmlMessage.SelectSingleNode("//pnp:MessageTypeSearchRequest", nsmgr);
            request = (MessageTypeSearchRequest)common.Utilities.DeserializeObject(searchNode.OuterXml, typeof(MessageTypeSearchRequest));

            typeList = business.MessageTypeManager.MessageTypeSearch(request);

            resultMsgType = (int)MessageTypes.MessageTypeSearchResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(typeList, typeof(MessageTypesList)));
        }

        //MessageTypeSelectItemRequest 1056
			public static void MessageTypeSelectItem(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				MessageType messageType = new MessageType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				//HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                Utilities.CreateUser(userCredentials, nsmgr);

				String messageTypeID = xmlMessage.SelectSingleNode("//pnp:MessageTypeID", nsmgr).InnerText;
				messageType = business.MessageTypeManager.MessageTypeSelectItem(messageTypeID);  
											
				resultMsgType = (int)MessageTypes.MessageTypeSelectItemResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(messageType, typeof(MessageType)));
			}

            //MessageTypeCheckIn 1045
			public static void MessageTypeUnlock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{				
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
				String messageTypeID = xmlMessage.SelectSingleNode("//pnp:MessageTypeID", nsmgr).InnerText;
				//String status = business.MessageTypeManager.MessageTypeUnlock(messageTypeID, currentUser.HSDIdentity.UserId);
                business.MessageTypeManager.MessageTypeUnlock(messageTypeID, currentUser.HSDIdentity.UserId);  
											
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //MessageTypeCheckinAll 1048
			public static void MessageTypeUnlockAll(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{				
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   
				
                //String status = business.MessageTypeManager.MessageTypeUnlockAll(currentUser.HSDIdentity.UserId);
                business.MessageTypeManager.MessageTypeUnlockAll(currentUser.HSDIdentity.UserId);
							
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //MessageTypeCheckOut 1046
			public static void MessageTypeLock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				MessageType messageType = new MessageType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				String messageTypeID = xmlMessage.SelectSingleNode("//pnp:MessageTypeID", nsmgr).InnerText;
                //String status = business.MessageTypeManager.MessageTypeLock(messageTypeID, currentUser.HSDIdentity.UserId);
                business.MessageTypeManager.MessageTypeLock(messageTypeID, currentUser.HSDIdentity.UserId);

				messageType = business.MessageTypeManager.MessageTypeSelectItem(messageTypeID);  
											
				resultMsgType = (int)MessageTypes.MessageTypeCheckOutResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(messageType, typeof(MessageType)));
			}

            //MessageTypeInsert 1049
			public static void MessageTypeInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				MessageType messageType = new MessageType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

			    XmlNode messageTypeNode = xmlMessage.SelectSingleNode("//pnp:MessageType", nsmgr);
                messageType = (MessageType)common.Utilities.DeserializeObject(messageTypeNode.OuterXml, typeof(MessageType));

                //String status = business.MessageTypeManager.MessageTypeInsert(messageType, currentUser.HSDIdentity.UserId);
                business.MessageTypeManager.MessageTypeInsert(messageType, currentUser.HSDIdentity.UserId);

                //if (status == "0")
                //    messageType = business.MessageTypeManager.MessageTypeSelectItem(messageType.MessageTypeID.ToString());

				resultMsgType = (int)MessageTypes.MessageTypeInsertResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(messageType, typeof(MessageType)));
			}

            //MessageTypeUpdate 1051
			public static void MessageTypeUpdate(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				MessageType messageType = new MessageType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);

                XmlNode messageTypeNode = xmlMessage.SelectSingleNode("//pnp:MessageType", nsmgr);
                messageType = (MessageType)common.Utilities.DeserializeObject(messageTypeNode.OuterXml, typeof(MessageType));

                //String status = business.MessageTypeManager.MessageTypeUpdate(messageType, currentUser.HSDIdentity.UserId);
                business.MessageTypeManager.MessageTypeUpdate(messageType, currentUser.HSDIdentity.UserId);

                messageType = business.MessageTypeManager.MessageTypeSelectItem(messageType.MessageTypeID.ToString());

                resultMsgType = (int)MessageTypes.MessageTypeUpdateResult;
                resultXmlMessage = new XmlDocument();
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(messageType, typeof(MessageType)));
			}

            //MessageTypeDelete    1053     
			public static void MessageTypeDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{				
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				String messageTypeID = xmlMessage.SelectSingleNode("//pnp:MessageTypeID", nsmgr).InnerText;
				//String status = business.MessageTypeManager.MessageTypeDelete(messageTypeID, currentUser.HSDIdentity.UserId);
                business.MessageTypeManager.MessageTypeDelete(messageTypeID, currentUser.HSDIdentity.UserId);  
															
                resultMsgType = 0;
				resultXmlMessage = new XmlDocument();
			}
       
        }
}
